package com.xish.util;

import org.joda.time.DateTime;
import org.joda.time.DateTimeConstants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class DateTimeUtil {

    private static final Logger LOG = LoggerFactory.getLogger(DateTimeUtil.class);

    public static DateTime thisWeekSaturday() {
        return thisWeekSaturday(new DateTime());
    }

    public static DateTime thisWeekSaturday(DateTime dateTime) {
        LOG.debug("input datetime: {}", dateTime.toString("yyyy-MM-dd HH:mm:ss"));

        int dayOfWeek = dateTime.getDayOfWeek();
        int diff = DateTimeConstants.SATURDAY - dayOfWeek;
        DateTime saturday = dateTime.plusDays(diff).withTime(0, 0, 0, 0);
        LOG.debug("this week saturday: {}", saturday.toString("yyyy-MM-dd HH:mm:ss"));

        return saturday;
    }

    public static DateTime nextWeekMonday() {
        return nextWeekMonday(new DateTime());
    }

    public static DateTime nextWeekMonday(DateTime dateTime) {
        if (dateTime == null) {
            dateTime = new DateTime();
        }
        LOG.debug("input datetime: {}", dateTime.toString("yyyy-MM-dd HH:mm:ss"));

        int dayOfWeek = dateTime.getDayOfWeek();
        int diff = DateTimeConstants.DAYS_PER_WEEK + DateTimeConstants.MONDAY - dayOfWeek;
        DateTime monday = dateTime.plusDays(diff).withTime(0, 0, 0, 0);
        LOG.debug("next week monday: {}", monday.toString("yyyy-MM-dd HH:mm:ss"));

        return monday;
    }

}
